package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-13 21:32
 **/
@Service
@Transactional
public class LabelService {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;


    public List<Label> findAll() {
        return labelDao.findAll();
    }

    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    public void add(Label label) {
        label.setId(idWorker.nextId() + "");
        labelDao.save(label);
    }

    public void update(Label label) {
        labelDao.save(label);
    }

    public Page<Label> findSearch(Map<String, Object> searchMap, int page, int size) {
        return labelDao.findAll(createSpecification(searchMap), PageRequest.of(page - 1, size));
    }

    private Specification<Label> createSpecification(Map<String, Object> searchMap){
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if(searchMap.get("labelname") != null && !"".equals(searchMap.get("labelname"))){
                    predicateList.add(cb.like(root.get("labelname").as(String.class), "%" + (String)searchMap.get("labelname") + "%"));
                }

                if(searchMap.get("state") != null && !"".equals(searchMap.get("state"))){
                    predicateList.add(cb.equal(root.get("state").as(String.class), (String)searchMap.get("state")));
                }

                if(searchMap.get("recommend") != null && !"".equals(searchMap.get("recommend"))){
                    predicateList.add(cb.equal(root.get("recommend").as(String.class), (String)searchMap.get("recommend")));
                }


                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    public void deleteById(String id) {
        labelDao.deleteById(id);
    }

    public List<Label> findSearch(Map<String, Object> searchMap) {
        return labelDao.findAll(createSpecification(searchMap));
    }
}
